package C;

import java.util.Arrays;
import java.util.Scanner;

public class C39 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[] nums = new int[n];
        for (int i=0;i<n;i++) {
            nums[i] = i;
        }

        int[] tar = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        for (int i=1;i<tar.length;i++) {
            nums[tar[i]] = nums[tar[0]];
        }

        int[][] guanxi = new int[n][n];
        for (int i=0;i<n;i++) {
            guanxi[i] = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        }
        System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.deepToString(guanxi));
        System.out.println(getResult(guanxi,nums,n,tar));
    }
    public static int getResult(int[][] guanxi,int[] nums,int n,int[] tar) {
        for (int i=0;i<n;i++) {
            for (int j=i+1;j<n;j++) {
                if (guanxi[i][j] == 1) {
                    nums[root(j,nums)] = root(i,nums);
//                    nums[i] = nums[j];
                }
            }
        }
        System.out.println(Arrays.toString(nums));
        int count=0;
        int target = root(tar[0],nums);
        System.out.println("target="+target);
        for (int i=0;i<n;i++) {
            if (root(i,nums) == target) {
                count++;
            }
        }
        count-=tar.length;
        return count;
    }

    public static int root(int i,int[] nums) {
        if (nums[i]!=i) {
            return nums[i] = root(nums[i],nums);
        }
        return i;
    }
}
